package core
{
	import com.greensock.TweenLite;
	
	import core.view.DownLoadListView;
	
	import flash.events.KeyboardEvent;
	import flash.system.Security;
	import flash.text.TextField;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;
	import org.aisy.display.USprite;

	public class Main extends USprite
	{
		
		public function Main()
		{
			try {
				Security.allowDomain("*");
				Security.allowInsecureDomain("*");
			}
			catch (error:*) {}
		}
		
		public function connect(im:*):void
		{
			initPlayer();
			im = null;
		}
		
		private function __import():void
		{
			TweenLite;
		}
		
		private function initPlayer():void
		{
			if (Ais.IMain.Swf.hasKey("iplayer_0") === -1) return;
			
			__addEvent();
		}
		
		private function __addEvent():void
		{
			Ais.IMain.stage.addEventListener(KeyboardEvent.KEY_DOWN, __stageKeyHandler);
		}
		
		private function __removeEvent():void
		{
			Ais.IMain.stage.removeEventListener(KeyboardEvent.KEY_DOWN, __stageKeyHandler);
		}
		
		private function __stageKeyHandler(e:KeyboardEvent):void
		{
			if (e.target is TextField) return;
			if (e.keyCode === 68) {
				if (TEvent.hasTrigger("DOWNLOAD_LIST")) {
					TEvent.trigger("UP_WINDOW_NEW", "CLEAR", {"name": "DOWNLOAD_LIST"});
				}
				else {
					new DownLoadListView();
				}
			}
			e = null;
		}
		
		override public function clear():void
		{
			__removeEvent();
			super.clear();
		}
		
	}
}